package setting

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

var (
	Cfg *ini.File

	RunMode string

	HTTPPort     int
	ReadTimeOut  time.Duration
	WriteTimeOut time.Duration

	PageSize  int
	JwtSecret string
)

/**
init函数初始化  读取ini配置只有两个方法  ini.Load   （*ini.File）.Section
*/
func init() {
	var err error
	Cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		log.Fatalf("fail to load app.ini, err v%", err)
	}
	LoadBase()
	LoadServer()
	LoadApp()

}

func LoadBase() {
	RunMode = Cfg.Section("").Key("RunMode").Value()
}
func LoadServer() {

	sec, err := Cfg.GetSection("server")
	if err != nil {
		log.Fatalf("Fail to get section 'server': %v", err)
	}

	HTTPPort = sec.Key("HttpPort").MustInt(8000)
	ReadTimeOut = time.Duration(sec.Key("ReadTimeout").MustInt(60)) * time.Second
	WriteTimeOut = time.Duration(sec.Key("WriteTimeout").MustInt(60)) * time.Second
}

func LoadApp() {
	sec, err := Cfg.GetSection("app")
	if err != nil {
		log.Fatalf("Fail to get section 'app': %v", err)
	}
	JwtSecret = sec.Key("JwtSecret").Value()
	PageSize = sec.Key("PageSize").MustInt(10)
}
